package com.teamwhole.common.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.teamwhole.common.config.Global;

public class FileUpload {
	
	private Logger logger = LoggerFactory.getLogger(getClass());

	private String fieldName = "file";
	private String dirName = "D:/file";
	private String savePath;
	private String fileName;
	private String fileExt;
	private String type = "other";

	public FileUpload() {
		super();
		String dirPath = Global.getConfig("dirPath");
		if (StringUtils.isNoneBlank(dirPath)) {
			this.dirName = dirPath;
		}
	}

	public FileUpload(String type) {
		this();
		this.type = type;
	}

	public String[] upload(FormDataMultiPart fdmp) {
		// 获取文件流
		FormDataBodyPart filePart = fdmp.getField(fieldName);
		// 把表单内容转换成流
		InputStream in = filePart.getValueAs(InputStream.class);
		FormDataContentDisposition disposition = filePart.getFormDataContentDisposition();
		this.fileName = disposition.getFileName();
		try {
			this.fileName = new String(fileName.getBytes("ISO8859-1"), "UTF-8");
			this.fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
			this.createDir();
			String newFileName = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss") + "_" + new Random().nextInt(1000) + "." + fileExt;
			FileUtils.copyInputStreamToFile(in, new File(this.dirName + File.separator + savePath, newFileName));
			
			String[] infos = new String[3];
			infos[0] = this.fileName;
			infos[1] = newFileName;
			infos[2] = (savePath + File.separator + newFileName);
			
			logger.debug("---------------------------------------");
			logger.debug("fileName:" + infos[0]);
			logger.debug("newFileName:" + infos[1]);
			logger.debug("savePath:" + infos[2]);
			logger.debug("---------------------------------------");
			
			return infos;
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		return null;
	}

	private void createDir() {
		String year = DateUtils.getYear();
		String month = DateUtils.getMonth();
		String day = DateUtils.getDay();
		
		StringBuilder sb = new StringBuilder(type);
		sb.append(File.separator).append(fileExt);
		sb.append(File.separator).append(year);
		sb.append(File.separator).append(month);
		sb.append(File.separator).append(day);
		// 不包含根目录
		this.savePath = sb.toString();
		sb.insert(0, File.separator).insert(0, this.dirName);

		File dir = new File(sb.toString());
		if (!dir.exists()) {
			dir.mkdirs();
		}
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getDirName() {
		return dirName;
	}

	public void setDirName(String dirName) {
		this.dirName = dirName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileExt() {
		return fileExt;
	}

	public void setFileExt(String fileExt) {
		this.fileExt = fileExt;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
